import random
from django.core.validators import MinValueValidator, MaxValueValidator
# Create your models here.

from django.db import models
# 引用内置的user模型
from django.contrib.auth.models import User


# 集成内置的user模型,并添加新的字段
class TeacherProfile(models.Model):
    ############# 字段 ##########
    GENDER_CHOICE = (
        ("male", "男"),
        ("female", "女"),
    )
    TITLE_CHOICE = (
        ('unknown', '未评级'),
        ('assistant', '助教'),
        ('lecturer', '讲师'),
        ('associate_professor', '副教授'),
        ('professor', '教授'),
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)  #

    teacher_id = models.IntegerField(unique=True, db_index=True, default=random.randint(1, 1000), help_text='教职工号',
                                     verbose_name='教职工号码')

    name = models.CharField(max_length=30, verbose_name='真实名字', help_text='真实名字', default='')
    gender = models.CharField(choices=GENDER_CHOICE, default='female', max_length=6, verbose_name='性别', help_text='性别')
    mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name='手机号')
    title = models.CharField(choices=TITLE_CHOICE, default='unknown', max_length=20, verbose_name='职称', help_text='职称')
    is_delete = models.BooleanField(default=False, verbose_name='是否任教')

    class Meta:
        verbose_name = "教师信息"
        verbose_name_plural = "教师信息"

    def __str__(self):
        return self.name


class Course(models.Model):
    ########### 外键 ###########
    teacher = models.ForeignKey(verbose_name='任课教师', help_text='任课教师', to='TeacherProfile',
                                on_delete=models.CASCADE)

    ########### 字段 ###########
    name = models.CharField(max_length=30, verbose_name='课程名称', help_text='课程名称')
    credit = models.IntegerField(verbose_name='学分', help_text='学分')
    xueshi = models.IntegerField(verbose_name='学时', help_text='学时')

    class Meta:
        verbose_name = "课程信息"
        verbose_name_plural = "课程信息"

    def __str__(self):
        return self.name


class Scores(models.Model):
    ########### 外键 ###########
    course = models.ForeignKey(verbose_name='课程', to='Course', on_delete=models.CASCADE)
    student = models.ForeignKey(verbose_name='学生', to='Students', on_delete=models.CASCADE)

    ########### 字段 ###########
    value = models.IntegerField(verbose_name='成绩', help_text='成绩',
                                validators=(MinValueValidator(0), MaxValueValidator(100)), null=True, blank=True)

    class Meta:
        verbose_name = "成绩信息"
        verbose_name_plural = "成绩信息"

    def __str__(self):
        return self.student.name + str(self.value)


class Students(models.Model):
    ########### 外键 ###########
    score = models.ManyToManyField(to='Scores', blank=True)

    ########### 字段 ###########
    GENDER_CHOICE = (
        ("male", "男"),
        ("female", "女"),
    )
    GRADES_CHOICE = (
        ('freshman', '大一'),
        ('sophomore', '大二'),
        ('junior', '大三'),
        ('senior', '大四'),
    )

    name = models.CharField(max_length=30, verbose_name='学生姓名', help_text='学生姓名')
    gender = models.CharField(choices=GENDER_CHOICE, default='female', max_length=6, verbose_name='性别', help_text='性别')
    grades = models.CharField(choices=GRADES_CHOICE, max_length=10, verbose_name='年级', help_text='年级')

    class Meta:
        verbose_name = "学生信息"
        verbose_name_plural = "学生信息"

    def __str__(self):
        return self.name
